#include "gvsocket.h"
#include "gvcp.h"

/**
 * Application enumeration APIs
 */
 
int gv_enum_discovery (void)
{
    int                sockfd;
    struct sockaddr_in sockaddr;
    int                broadcast;
    GvcpPacket *       cmd_packet;
    int                bytes_sent;
    
    broadcast = 1;
    if (gv_create_socket (&sockfd, &sockaddr, broadcast) < 0)
        return -1;
    
    cmd_packet = gvcp_packet_new_discovery_cmd (0);
    if (cmd_packet == NULL)
        return -2;
    
    bytes_sent = sendto (sockfd, cmd_packet, sizeof(GvcpHeader), 0, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
    if (bytes_sent != sizeof(GvcpHeader)) {
        PRINT_ERROR ("Error occurs while broadcasting discovery command packet\n");
        return -3;
    }
    
    return 0;
}

int gv_enum_discovery_ack_listener (void)
{
    int                sockfd;
    struct sockaddr_in sockaddr;
    int                broadcast;
    uint8_t            recv_buf[sizeof(GvcpHeader) + sizeof(GvcpDataDiscoveryAck)];
    int                bytes_recv;
    
    broadcast = 0;
    if (gv_create_socket (&sockfd, &sockaddr, broadcast) < 0)
        return -1;
    
    // recvfrom
    
    // dispatch to ack handler
}

/**
 * Device enumeration APIs
 */
int gv_enum_discovery_cmd_listener (void)
{
    int                sockfd;
    struct sockaddr_in sockaddr;
    int                broadcast;
    uint8_t            recv_buf[sizeof(GvcpHeader)];
    int                bytes_recv;
    
    broadcast = 1;
    if (gv_create_socket (&sockfd, &sockaddr, broadcast) < 0)
        return -1;
    
    // recvfrom
    
    // dispatch to cmd handler
}